<?php

function formatDate($datecreated)
{

	$subjectdate = $datecreated;

	$timeconv = strtotime($subjectdate);
	$year = "years";
	$month="months";
	$day="days";
	$hour = "hours";
	$min = "minutes";
	$sec = "seconds";
	
	$diff = time() - $timeconv;

	if ($diff>31104000)//years
	{
		$diffyears = intval($diff/31104000);
		if ($diffyears==1)
			$year="year";
		$diffmonths = intval(($diff%31104000)/2592000);
		if ($diffmonths==1)
			$month="month";
		$subjectdate = "$diffyears $year and $diffmonths $month ago.";
	}
	elseif ($diff>2592000)//months
	{
		$diffmonths= intval($diff/2592000);
		if ($diffmonths==1)
			$month="month";
		$diffdays = intval(($diff%2592000)/86400);
		if ($diffdays==1)
			$day="day";
		$subjectdate = "$diffmonths $month and $diffdays $day ago.";
	}
	elseif ($diff>86400)//days
	{
		$diffdays = intval($diff/86400);
		if ($diffdays==1)
			$day="day";
		$diffhours = intval(($diff%86400)/3600);
		if ($diffhours==1)
			$hour="hour";
		$subjectdate = "$diffdays $day and $diffhours $hour ago.";
	}
	elseif ($diff>3600)//hours
	{
		$diffhours = intval($diff/3600);
		if ($diffhours==1)
			$hour="hour";
		$diffminutes = intval(($diff%3600)/60);
		if ($diffminutes==1)
			$min="minute";
		$subjectdate = "$diffhours $hour and $diffminutes $min ago.";
	}
	elseif ($diff>60)//minutes
	{
		$diffminutes = intval($diff/60);		
		if ($diffminutes==1)
			$min="minute";
		$diffseconds = intval($diff%60);
		if ($diffseconds==1)
			$sec="second";
		$subjectdate = "$diffminutes $min and $diffseconds $sec ago.";
	}
	else
	{
        	//$diff is in seconds.
		if ($diff==1)
			$sec="second";
		$subjectdate = "$diff $sec ago";
	}

	return $subjectdate;
}
